package com.example.administrator.school.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.administrator.school.ui.GlideImageLoader;
import com.example.administrator.school.R;
import com.example.administrator.school.adapter.HomeAdapter;
import com.example.administrator.school.bean.HotBean;
import com.xiumiing.uilibrary.base.BaseFragment;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.xiumiing.uilibrary.Constans.TITLE;


public class HomeFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.RecyclerView)
    RecyclerView mRecycler;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout mSwipeRefresh;
    Unbinder unbinder;
    private ViewHolder mHolder;
    private int mPage = 0;
    private HomeAdapter mAdapter;
    private List<HotBean> mData = new ArrayList<>();

    public static HomeFragment newInstance(String title) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(TITLE, title);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        unbinder = ButterKnife.bind(this, view);
        View headView = inflater.inflate(R.layout.home_fragement_head, null);
        mHolder = new ViewHolder(headView);//头部View 持有者

        mSwipeRefresh.setOnRefreshListener(this);
        mSwipeRefresh.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));
        //设置广告栏
        setBanner();
        //内容
        setHotContent();
        return view;
    }

    /**
     * 设置广告栏
     */
    private void setBanner() {
//        mHolder.mBanner.setOnBannerListener(position -> {
////            if (!UserHelper.isLogin()) {
////                LoginActivity.start(true);
////                return;
////            }
//            String link = mBannerList.get(position).getLink();
//            startActivity(new Intent(getContext(), WebActivity.class).putExtra(URL, link));
//        });
        mImages.add("http://p1.ifengimg.com/a/2016_43/0c546c76eb49fab_size123_w690_h367.jpg");
        mImages.add("http://img1.imgtn.bdimg.com/it/u=144945426,4096725283&fm=26&gp=0.jpg");
        mImages.add("http://p1.ifengimg.com/a/2016_43/0c546c76eb49fab_size123_w690_h367.jpg");
        mHolder.mBanner.setImageLoader(new GlideImageLoader());
        mHolder.mBanner.setImages(mImages);
        mHolder.mBanner.start();
    }

    /**
     * 内容
     */
    private void setHotContent() {
        mRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new HomeAdapter(mData);
        mAdapter.addHeaderView(mHolder.mBase, 0);
        mAdapter.setOnLoadMoreListener(this, mRecycler);
        mRecycler.setAdapter(mAdapter);
        onRefresh();
    }

    //如果你需要考虑更好的体验，可以这么操作
    @Override
    public void onStart() {
        super.onStart();
        //开始轮播
        mHolder.mBanner.startAutoPlay();
    }

    @Override
    public void onStop() {
        super.onStop();
        //结束轮播
        mHolder.mBanner.stopAutoPlay();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        mPage = 0;
        loadData();
    }

    @Override
    public void onLoadMoreRequested() {
        mPage++;
        loadData();
    }

    private void loadData() {
        if (mPage==0) {
            mData.clear();
        }
        for (int i = 0; i < 10; i++) {
            mData.add(new HotBean());
        }
        mAdapter.notifyDataSetChanged();

        mRecycler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefresh.setRefreshing(false);
                mAdapter.loadMoreComplete();
            }
        },500);

    }


    static class ViewHolder {
        @BindView(R.id.banner)
        Banner mBanner;
        View mBase;

        ViewHolder(View view) {
            mBase = view;
            ButterKnife.bind(this, view);
        }
    }

    private List<String> mImages = new ArrayList<>();
}
